package com.ruoyi.web.controller.sitefee;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.PlanToLog;
import com.ruoyi.domain.SiteFeeSchedule;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.ISiteFeeScheduleService;
import com.ruoyi.system.domain.ProFileBlueprint;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 工地安全质量费用Controller
 *
 * @author ruoyi
 * @date 2020-06-18
 */
@Controller
@RequestMapping("/ruoyi/schedule")
public class SiteFeeScheduleController extends BaseController {
    private String prefix = "ruoyi/schedule";

    @Autowired
    private ISiteFeeScheduleService siteFeeScheduleService;

    @RequiresPermissions("ruoyi:schedule:view")
    @GetMapping()
    public String schedule() {
        return prefix + "/schedule";
    }

    /**
     * 详情页面点击该页面传递一个参数
     * @param bId
     * @param map
     * @return
     */
    @RequiresPermissions("ruoyi:schedules:view")
    @GetMapping("/schedules/{bId}")
    public String logs(@PathVariable("bId") Long bId, ModelMap map) {
        SiteFeeSchedule siteFeeSchedule = siteFeeScheduleService.selectSiteFeeScheduleById(bId);
        map.put("siteFeeSchedule", siteFeeSchedule);
        return prefix + "/schedules";
    }

    /**
     * 查询工地安全质量费用列表
     */
    @RequiresPermissions("ruoyi:schedule:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SiteFeeSchedule siteFeeSchedule) {
        startPage();
        List<SiteFeeSchedule> list = siteFeeScheduleService.selectSiteFeeScheduleList(siteFeeSchedule);
        return getDataTable(list);
    }



    /**
     * 查询总项目下对应的计划日志列表
     */
    @RequiresPermissions("ruoyi:log:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo lists(Long sid) {
        startPage();
        List<SiteFeeSchedule> list = siteFeeScheduleService.selectSiteFeeScheduleLists(sid);
        return getDataTable(list);
    }


    /**
     * 查询总项目下对应的计划日志列表
     */
    @RequiresPermissions("ruoyi:log:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long idss) {
        startPage();
        List<SiteFeeSchedule> list = siteFeeScheduleService.selectSiteFeeScheduleLists(idss);
        return getDataTable(list);
    }

    /**
     * 导出工地安全质量费用列表
     */
    @RequiresPermissions("ruoyi:schedule:export")
    @Log(title = "工地安全质量费用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SiteFeeSchedule siteFeeSchedule) {
        List<SiteFeeSchedule> list = siteFeeScheduleService.selectSiteFeeScheduleList(siteFeeSchedule);
        ExcelUtil<SiteFeeSchedule> util = new ExcelUtil<SiteFeeSchedule>(SiteFeeSchedule.class);
        return util.exportExcel(list, "schedule");
    }

    /**
     * 新增工地安全质量费用
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId", pId);
        return prefix + "/add";
    }

    /**
     * 新增保存工地安全质量费用
     */
    @RequiresPermissions("ruoyi:schedule:add")
    @Log(title = "工地安全质量费用", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, SiteFeeSchedule siteFeeSchedule) throws IOException {
        //上传文件路径
        String filePach = Global.getUploadPath();
        //上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePach, file);
        siteFeeSchedule.setsUploading(fileName);
        String name = ShiroUtils.getLoginName();
        siteFeeSchedule.setsTheHeir(name);
        siteFeeSchedule.setsTime(new Date());
        return toAjax(siteFeeScheduleService.insertSiteFeeSchedule(siteFeeSchedule));
    }

    /**
     * 修改工地安全质量费用
     */
    @GetMapping("/edit/{sId}")
    public String edit(@PathVariable("sId") Long sId, ModelMap mmap) {
        SiteFeeSchedule siteFeeSchedule = siteFeeScheduleService.selectSiteFeeScheduleById(sId);
        mmap.put("siteFeeSchedule", siteFeeSchedule);
        return prefix + "/edit";
    }

    /**
     * 修改保存工地安全质量费用
     */
    @RequiresPermissions("ruoyi:schedule:edit")
    @Log(title = "工地安全质量费用", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SiteFeeSchedule siteFeeSchedule) {
        return toAjax(siteFeeScheduleService.updateSiteFeeSchedule(siteFeeSchedule));
    }



    /**
     * 删除工地安全质量费用
     */
    @RequiresPermissions("ruoyi:schedule:remove")
    @Log(title = "工地安全质量费用", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(siteFeeScheduleService.deleteSiteFeeScheduleByIds(ids));
    }

    /**
     *
     * 下载功能
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("pUploading") String fAccessory, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(fAccessory, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }

    /**
     * 导出付款申请单合同费用
     */
    @Log(title = "工地安全质量", businessType = BusinessType.EXPORT)
    @RequiresPermissions("ruoyi:schedule:exports")
    @PostMapping("/exports")
    @ResponseBody
    public AjaxResult exports(SiteFeeSchedule siteFeeSchedule)
    {
        List<SiteFeeSchedule> list = siteFeeScheduleService.selectUserexports(siteFeeSchedule);
        ExcelUtil<SiteFeeSchedule> util = new ExcelUtil<SiteFeeSchedule>(SiteFeeSchedule.class);
        return util.exportExcel(list, "工地安全质量");
    }
}
